package com.mxy.myapplication.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.mxy.myapplication.R;
import com.mxy.myapplication.adapter.AdapterForDownloadMissionList;
import com.mxy.myapplication.pojo.DownloadMissionPojo;
import com.mxy.myapplication.util.DownloadMissionManageUtil;
import com.mxy.myapplication.util.IsWebCanBeUsed;
import com.mxy.myapplication.util.MyItemDecoration;
import com.mxy.myapplication.util.ShareData;
import com.mxy.myapplication.util.Util;

import java.io.File;

/**
 * 文件下载进度展示+通知栏进度展示+断点续传+不使用内置DownloadManager
 * 文件默认下载路径为getExternalCacheDir()
 *
 * @author 马晓勇
 */
public class DownLoadScheduleActivity extends BaseActivity implements View.OnClickListener {

    private static final String FILE_URL = "https://dldir1.qq.com/weixin/android/weixin707android1520.apk";
    private static final String NEWMISSION_MISSIONID_KEY = "downloadmissionid";
    /**
     * 下载任务数量上限
     */
    private static final int DOWNLOAD_MISSION_MAX = 3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_downloadschedule);
        initView();
    }

    private void initView() {

        findViewById(R.id.addMission).setOnClickListener(this);
        RecyclerView downloadMissionsRecyclerView = findViewById(R.id.downloadMissionsRecyclerView);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        downloadMissionsRecyclerView.setLayoutManager(linearLayoutManager);

        MyItemDecoration myItemDecoration = new MyItemDecoration(getResources(),
                R.color.palegreen,
                Util.dip2px(this, 1),
                Util.dip2px(this, 10),
                Util.dip2px(this, 10));

        downloadMissionsRecyclerView.addItemDecoration(myItemDecoration);
        downloadMissionsRecyclerView.setItemAnimator(new DefaultItemAnimator());
        AdapterForDownloadMissionList adapter = new AdapterForDownloadMissionList(this,
                DownloadMissionManageUtil.getMissionList());
        DownloadMissionManageUtil.setObserverAdapter(adapter);
        downloadMissionsRecyclerView.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.addMission) {
            addMission();
        }
    }

    /**
     * 添加新下载任务
     */
    private void addMission() {
        if (IsWebCanBeUsed.isWebCanBeUse(this)) {
            //暂时限制下载任务数量不能超过3个
            if (DownloadMissionManageUtil.getMissionList().size() >= DOWNLOAD_MISSION_MAX) {
                Util.showToast(this, "下载任务已达上限");
            } else {
                DownloadMissionPojo downloadMissionPojo = new DownloadMissionPojo();

                int missionId = ShareData.getShareIntData(NEWMISSION_MISSIONID_KEY) + 1;
                downloadMissionPojo.setMissionID(missionId);
                ShareData.setShareIntData(NEWMISSION_MISSIONID_KEY, missionId);

                downloadMissionPojo.setFileUrl(FILE_URL);
                downloadMissionPojo.setFileName(downloadMissionPojo.getMissionID() + "_WeChat.apk");

                File file =
                        new File(getExternalCacheDir() + File.separator + "apkfiles" + File.separator);
                if (!file.exists()) {
                    file.mkdirs();
                }
                downloadMissionPojo.setFilePath(file.getPath() + File.separator + downloadMissionPojo.getFileName());
                DownloadMissionManageUtil.addMission(downloadMissionPojo);
            }
        } else {
            Util.showToast(this, "当前网络不可用");
        }
    }
}